Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Class DbfTableView
	Inherits ListView
	Private mIndex As DbfIndex
	Private mTable As DbfTable

	Public Sub New()
		FullRowSelect = True
		GridLines = True
		HideSelection = False
		MultiSelect = False
		UseCompatibleStateImageBehavior = False
		View = System.Windows.Forms.View.Details
		VirtualMode = True
	End Sub

	Private Sub tableView1_ColumnClick(sender As Object, e As ColumnClickEventArgs)
	End Sub

	Protected Overrides Sub OnColumnClick(e As ColumnClickEventArgs)
		MyBase.OnColumnClick(e)

		If e.Column = 0 Then
			mIndex = Nothing
		Else
			SetIndex(mTable.Columns(e.Column))
		End If

	End Sub

	Private theListviewItem As ListViewItem

	Protected Overrides Sub OnRetrieveVirtualItem(e As RetrieveVirtualItemEventArgs)
        Dim columnCount As Integer = Me.Columns.Count
		Try
            Dim record As DbfRecord = GetRecordByRow(CType(e.ItemIndex, Int32))
			If record Is Nothing Then
				theListviewItem.Text = "#null"
				For i As Integer = 1 To columnCount - 1
					theListviewItem.SubItems(i).Text = "#null"
				Next
			Else
				theListviewItem.Text = "#" & (e.ItemIndex + 1)

				For i As Integer = 1 To columnCount - 1
					theListviewItem.SubItems(i).Text = ToString(record.GetField(i))
				Next

			End If
				'    tableView1.Text = ex.Message;
		Catch generatedExceptionName As Exception
		End Try
		e.Item = theListviewItem

		MyBase.OnRetrieveVirtualItem(e)
	End Sub

	Private Overloads Function ToString(p As Object) As String
		If p Is Nothing Then
			Return "<null>"
		End If
		Return p.ToString()
	End Function

    Private Function GetRecordByRow(rowNo As Int32) As DbfRecord
        Dim record As DbfRecord = Nothing
        If mIndex Is Nothing Then
            record = mTable.GetRecord(rowNo)
        Else
            record = mIndex.GetRecord(rowNo)
        End If
        Return record
    End Function


    Public Function GetRow(Of T)(ByVal rownumber As Int32, ByRef record As T) As Boolean
        Dim Row As DbfRecord = GetRecordByRow(rownumber)
        If Row Is Nothing Then
            Return False
        Else
            ' record = CType(Row, T)
            Return True
        End If
    End Function

	Public Property DbfTable() As DbfTable
		'(DbfTable<global::DemoApplication1.Individual> individuals)
		Get
			Return mTable
		End Get
		Set
			If mTable Is value Then
				Return
			End If
			mTable = value
			Initialize()
		End Set
	End Property

	Private Sub SetIndex(columnDefinition As DbfDotNet.Core.ColumnDefinition)
		Dim columnName As String = (columnDefinition.ColumnName)
        Dim sortOrder As DbfDotNet.SortOrder = New DbfDotNet.SortOrder(mTable, False)
		sortOrder.AddField(columnName)
		mIndex = mTable.GetIndex(columnName & ".ndx", sortOrder)
		RefreshGrid()
	End Sub

	Public Sub Initialize()
		Columns.Clear()
		VirtualListSize = 0
		If mTable Is Nothing Then
			Return
		End If

		Columns.Add("Record Number", 80)
		theListviewItem = New ListViewItem()


		For i As Integer = 1 To mTable.Columns.Count - 1
            Dim c As Core.ColumnDefinition = mTable.Columns(i)
			Columns.Add(c.ColumnName, 120)
			theListviewItem.SubItems.Add("#null")
		Next

		VirtualListSize = mTable.RecordCount
	End Sub

	Public Sub RefreshGrid()
		VirtualListSize = 0
		VirtualListSize = mTable.RecordCount
	End Sub

End Class
